package _022_2_15

type MinStack struct {
	min          int
	data         []int
	isContainMin bool
}

func Constructor() MinStack {
	return MinStack{}
}

func (this *MinStack) Push(x int) {
	this.data = append(this.data, x)
	if !this.isContainMin {
		this.isContainMin = true
		this.min = x
		return
	}

	if x < this.min {
		this.min = x
	}
}

func (this *MinStack) Pop() {
	topElement := this.data[len(this.data)-1]
	this.data = this.data[:len(this.data)-1]
	if topElement != this.min {
		return
	}
	if len(this.data) <= 0 {
		this.isContainMin = false
		return
	}

	min := this.data[0]
	for i := 1; i < len(this.data); i++ {
		if this.data[i] < min {
			min = this.data[i]
		}
	}

	this.min = min
}

func (this *MinStack) Top() int {
	return this.data[len(this.data)-1]

}

func (this *MinStack) Min() int {
	return this.min
}
